This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 08-05 05:25:10] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 08-05 05:25:10] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 08-05 05:25:10] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-05 05:25:10] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-05 05:25:10] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-05 05:25:10] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-05 05:25:10] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-05 05:25:10] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-05 05:25:10] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 08-05 05:25:10] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 08-05 05:25:10] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.762832, 'x2': 0.05379, 'x3': 0.392285, 'x4': 0.796379, 'x5': 0.031157, 'x6': 0.647694}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.091902, 0.1), 'l2norm': (1.186729, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.27612, 'x2': 0.629525, 'x3': 0.444911, 'x4': 0.454371, 'x5': 0.358273, 'x6': 0.711309}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.956286, 0.1), 'l2norm': (1.379647, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.806094, 'x2': 0.382641, 'x3': 0.250372, 'x4': 0.47032, 'x5': 0.501752, 'x6': 0.81408}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.207434, 0.1), 'l2norm': (1.243025, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.180023, 'x2': 0.017497, 'x3': 0.205575, 'x4': 0.18313, 'x5': 0.438224, 'x6': 0.472915}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-1.347352, 0.1), 'l2norm': (0.855001, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.185501, 'x2': 0.841099, 'x3': 0.429268, 'x4': 0.551702, 'x5': 0.551039, 'x6': 0.764576}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.152521, 0.1), 'l2norm': (1.405881, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.310013, 'x2': 0.506531, 'x3': 0.718359, 'x4': 0.826283, 'x5': 0.148719, 'x6': 0.302852}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.237514, 0.1), 'l2norm': (1.371599, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.987541, 'x2': 0.557855, 'x3': 0.163599, 'x4': 0.554898, 'x5': 0.070106, 'x6': 0.186211}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.133206, 0.1), 'l2norm': (1.648194, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.986605, 'x2': 0.855414, 'x3': 0.02174, 'x4': 0.364729, 'x5': 0.114455, 'x6': 0.152643}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (0.058827, 0.1), 'l2norm': (1.375658, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.07049, 'x2': 0.538641, 'x3': 0.551563, 'x4': 0.908765, 'x5': 0.230699, 'x6': 0.164288}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (0.076911, 0.1), 'l2norm': (1.31833, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.706315, 'x2': 0.599154, 'x3': 0.533844, 'x4': 0.012268, 'x5': 0.342721, 'x6': 0.828885}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.405902, 0.1), 'l2norm': (1.335626, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.026295, 'x2': 0.56009, 'x3': 0.459024, 'x4': 0.558193, 'x5': 0.278156, 'x6': 0.173846}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.200475, 0.1), 'l2norm': (0.860896, 0.1)}.
[INFO 08-05 05:25:10] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.870949, 'x2': 0.942673, 'x3': 0.373264, 'x4': 0.76923, 'x5': 0.345668, 'x6': 0.047489}.
[INFO 08-05 05:25:10] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.113071, 0.1), 'l2norm': (1.736593, 0.1)}.
[INFO 08-05 05:25:22] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.205029, 'x2': 0.155316, 'x3': 0.267507, 'x4': 0.213535, 'x5': 0.401723, 'x6': 0.552303}.
[INFO 08-05 05:25:22] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-2.415118, 0.1), 'l2norm': (0.858526, 0.1)}.
[INFO 08-05 05:25:27] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.205163, 'x2': 0.225909, 'x3': 0.29771, 'x4': 0.219653, 'x5': 0.386504, 'x6': 0.593997}.
[INFO 08-05 05:25:27] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-2.402577, 0.1), 'l2norm': (1.006338, 0.1)}.
[INFO 08-05 05:25:29] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.276477, 'x2': 0.197146, 'x3': 0.304371, 'x4': 0.255103, 'x5': 0.366947, 'x6': 0.562681}.
[INFO 08-05 05:25:29] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-2.523709, 0.1), 'l2norm': (0.747647, 0.1)}.
[INFO 08-05 05:25:32] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.232262, 'x2': 0.186387, 'x3': 0.401325, 'x4': 0.232099, 'x5': 0.37096, 'x6': 0.5585}.
[INFO 08-05 05:25:32] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-2.871136, 0.1), 'l2norm': (0.756254, 0.1)}.
[INFO 08-05 05:25:33] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.235264, 'x2': 0.1783, 'x3': 0.452287, 'x4': 0.174796, 'x5': 0.308852, 'x6': 0.55357}.
[INFO 08-05 05:25:33] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.787148, 0.1), 'l2norm': (0.862133, 0.1)}.
[INFO 08-05 05:25:35] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.256209, 'x2': 0.192618, 'x3': 0.480516, 'x4': 0.203618, 'x5': 0.428033, 'x6': 0.552611}.
[INFO 08-05 05:25:35] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.413133, 0.1), 'l2norm': (0.76919, 0.1)}.
[INFO 08-05 05:25:36] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.1925, 'x2': 0.164668, 'x3': 0.410855, 'x4': 0.27519, 'x5': 0.302414, 'x6': 0.558958}.
[INFO 08-05 05:25:36] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-3.052064, 0.1), 'l2norm': (0.838336, 0.1)}.
[INFO 08-05 05:25:37] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.163233, 'x2': 0.212094, 'x3': 0.407397, 'x4': 0.282812, 'x5': 0.277441, 'x6': 0.523683}.
[INFO 08-05 05:25:37] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.685877, 0.1), 'l2norm': (0.83471, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 08-05 05:25:37] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 46.03 seconds.